package com.cx.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * @Author:cxy
 * @Date:2022/4/9 15:30
 */
public class JdkDynamicProxy implements InvocationHandler {

    private Object tragetObj;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if(Object.class.equals(method.getDeclaringClass())){
            return method.invoke(this,args);
        }
        System.out.println("开始");
        System.out.println("proxy = " + proxy + ", method = " + method + ", args = " + Arrays.toString(args));
        Object invoke = method.invoke(tragetObj, args);
        System.out.println("结束");
        return invoke;
    }

    public Object getInstance(Object o){
        this.tragetObj = o;
        return Proxy.newProxyInstance(o.getClass().getClassLoader(),o.getClass().getInterfaces(),this);
    }

}
